package com.zlm.user.partner.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.dto.HxUserDto;
import com.zlm.common.dto.userPartner.SysPartnerDto;
import com.zlm.common.entity.SysPartnerDO;
import com.zlm.common.util.DateTimeUtils;
import com.zlm.common.util.MD5Utils;
import com.zlm.user.partner.dao.SysPartnerDao;
import com.zlm.user.partner.service.SysPartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: duyufu
 * @Date: 2019/7/10 11:21
 */
@Service
public class SysPartnerServiceImpl extends AbstractBaseService<SysPartnerDao, SysPartnerDO> implements SysPartnerService {

    @Autowired
    @Override
    public void setBaseDao(SysPartnerDao sysPartnerDao) {
        this.baseDao = sysPartnerDao;
    }

    @Override
    public boolean isExistPart(String mobile) {
        int m = baseDao.isExistPart(mobile);
        if (m > 0) {
            return true;
        }
        return false;
    }

    @Override
    public SysPartnerDto queryPartner(Map<String, Object> map) {
        List<SysPartnerDto> sysPartnerList = baseDao.queryPartner(map);
        if (null != sysPartnerList && sysPartnerList.size() == 1) {
            SysPartnerDto sysPartnerDto = sysPartnerList.get(0);
            return sysPartnerDto;
        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updatePassword(String mobile, String password) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("mobile",mobile);
        map.put("partPwd", MD5Utils.getStringMD5(password));
        return baseDao.updatePassword(map);
    }

    @Override
    public String queryPartnerPassword(Integer partId) {
        return baseDao.queryPartnerPassword(partId);
    }

    @Override
    public void updatePasswordByPartId(Integer partId, String password) {
        baseDao.updatePasswordByPartId(partId,password);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setPassword(Integer partId, String password) {
        SysPartnerDO sysPartner = new SysPartnerDO();
        sysPartner.setPartId(partId);
        sysPartner.setPartPwd(MD5Utils.getStringMD5(password));
        sysPartner.setLastModifyUser(partId);
        sysPartner.setLastModifyTime(DateTimeUtils.getCurrentDateTime());
        baseDao.updateSysPart(sysPartner);
    }

    @Override
    public void changePhone(Integer partId, String mobile) {
        SysPartnerDO sysPartner = new SysPartnerDO();
        sysPartner.setPartId(partId);
        sysPartner.setMobile(mobile);
        sysPartner.setLastModifyUser(partId);
        sysPartner.setLastModifyTime(DateTimeUtils.getCurrentDateTime());
        baseDao.updateSysPart(sysPartner);
    }

    @Override
    public void updateHxPasswordByPartId(Integer partId, String hxID, String hxPassword) {
        baseDao.updateHxPasswordByPartId(partId,hxID,hxPassword);
    }

    @Override
    public SysPartnerDto queryPartnerById(Integer partId) {
        return baseDao.queryPartnerById(partId);
    }

    @Override
    public HxUserDto queryHxUser(String hxId) {
        return baseDao.queryHxUser(hxId);
    }

    @Override
    public List<HxUserDto> searchFriend(String keyword) {
        return baseDao.searchFriend(keyword);
    }
}
